<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/

$id=$_GET['id'];

	//$pathdb = "uploads/download"; // Duong dan ghi vao database		
	$imagepath="../uploads/product/"; // Duong dan de upload Image
	$upload_allow_extension = "jpg,png,gif"; // Cac phan mo rong duoc cho phep

if (isset($_POST['butSave'])) {
	$published = $_POST['published'];
	$title = $_POST['title'];
	$alias = vietdecode($title);
	$intro = $_POST['intro'];
	$price = $_POST['price'];
	$date_add = mktime();
	$catid = $_POST['catid'];
	$type = $_POST['type'];
	//$cat = getC($catid);
	//$catlink = 'download/'.$cat->alias;
	//$cattitle = $cat->title;
	$hit = $_POST['hit'];
	
	$err="";	
	if ($title=="") $err .="Xin nhập tiêu đề<br>";	
	if ($intro =="") $err .="Xin nhập intro bằng<br>";	
	
	
	if ($err==''){
		/* Cap nhat hoac them moi */
		if (!empty($_POST['id'])) {
			$oldid = $_POST['id'];
			$sql = "update product set  title='".$title."',alias='".$alias."', intro='".$intro."',hit='".$hit."',";
			$sql.= "published='".$published."',price='".$price."', ";
			$sql.= "catid='".$catid."',type='".$type."' where id='".$oldid."'";
		}else {
			// Insert data
			$sql = "insert into product (title,alias,price,cattitle,intro,published,catid,type,hit,date_add)";
			$sql.= "values ('".$title."','".$alias."','".$price."'";
			$sql.= ",'".$intro."','".$published."','".$catid."','".$type."','".$hit."','".$date_add."')";
		}
		/* EOF - Cap nhat hoac them moi */
		if(mysql_query($sql)){ // Neu thuc hien query thanh cong
		
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			
		
			
		//Upload image here
		$filename="";
		if(is_uploaded_file($_FILES['img']['tmp_name']))
		{
			//Check file extension
			if (check_upload_extension($_FILES['img']['name'],$upload_allow_extension) == 1){
			$filename = generate_name('product',$oldid,$_FILES['img']['name']);
			move_uploaded_file($_FILES['img']['tmp_name'],$imagepath .'large/'. $filename);
			resize_image($imagepath.'large/'.$filename,$imagepath.'large/'.$filename,500,650,80);
			
			resize_image($imagepath.'large/'.$filename,$imagepath.'small/'.$filename,75,75,100);
			//resize_image($imagepath.'large/'.$filename,$imagepath.'medium/'.$filename,120,200,100);
			//resize_image($imagepath.$filename,$imagepath.'large/'.$filename,500,480,100);

			$_POST["img"] = $filename;
					
			$sqlUpdateField = " image ='".$filename."'";
			
				if($sqlUpdateField!='')
				{
					$sqlUpdate = "update product set $sqlUpdateField where id='".$oldid."'";
					mysql_query($sqlUpdate);
				}
			}
		} else {
			$_POST["img"] = "";
		}
		// Eof Upload image
		} // Neu thuc hien Query thanh cong
		else {
			$err =  "Không th&#7875; c&#7853;p nh&#7853;t";
		}
		
	} // if ($err=='')
	
	if ($err==''){
	//$strSQL = "SELECT * from download order by date_add DESC limit 7";
	//echo homelist($strSQL,'../modules/html/download.htm');
	echo '<script>window.location="index.php?act=product&code=1"</script>';
	}else { 
	echo "<p align=center class='red'>".$err."</p>";
	}

} 
			
 action('Product');
if (isset($_GET['id'])){

	$result=mysql_query("SELECT * FROM product WHERE id=$id");
	$row=mysql_fetch_object($result);
	$title=$row->title;
	$image=$row->image;
	$type=$row->type;
	$intro=$row->intro;
	$published=$row->published;
	$fileurl=$row->fileurl;
	$hit=$row->hit;
	$catid=$row->catid;
	$mid=$row->mid;
	$parent=$catid;
	}
?>
  <table border="0" cellpadding="3" cellspacing="3" bordercolor="#CCCCCC">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="hidden" name="act" value="product_m">
<input type="hidden" name="id" value="<? echo $_REQUEST['id']; ?>">
<input type="hidden" name="page" value="<? echo $_REQUEST['page']; ?>">

	<tr>
      <td width="20%" align="right" valign="middle">Download name: </td>
      <td width="76%">  <input name="title" type="text" value="<?=$title;?>" size="50"></td>
    </tr>

	<tr>
      <td align="right" valign="top">Description:</td>
      <td valign="top" ><textarea name="intro" rows="4" id="intro"><?=$intro;?></textarea></td>
	</tr>
	 
	<tr>
	  <td align="right">Category: </td>
	  <td ><select size="1" name="catid">
        <?
		echo "<option value='0'>Root</option>";
		$cats=getListCat('','product_categories');
		foreach ($cats as $cat)
		{
			if ($cat[0]==$catid)
				echo "<option value=".$cat[0]." selected>".$cat[1]."</option>";
			else
				echo "<option value=".$cat[0].">".$cat[1]."</option>";
		}
		?>
      </select></td>
	</tr>
	<tr>
	  <td align="right" valign="bottom">Loại hình </td>
	  <td >
	  <? if($type==0){?>
	  <input name="type" type="radio" value="0" checked="checked" />
	    Ngành Nông nghiệp 
          <input name="type" type="radio" value="1" /> 
        Ngành Thực phẩm
	 <? } else {?>
	 
	  <input name="type" type="radio" value="0" />
	    Ngành Nông nghiệp 
          <input name="type" type="radio" value="1" checked="checked" /> 
        Ngành Thực phẩm	 
	 <? } ?>	
		 </td>
    </tr>
	<tr>
      <td align="right" valign="bottom">Image:</td>
      <td ><? if($image!='') {?>
	  <a href="../<?=$image;?>" target="_blank">
	  <img src="../uploads/product/small/<?=$image;?>" border="0" /></a><br /><? } ?>
	  <input name="img" type="file" id="img"></td>
	</tr>
	<tr>
	  <td align="right">Giá: </td>
	  <td ><input name="price" type="text" id="price" value="<?=$price;?>" size="50" /> VND</td>
	</tr>
	<tr>
	  <td align="right">Hit: </td>
	  <td ><input name="hit" type="text" id="hit" value="<?=$hit;?>" /></td>
	</tr>
	<tr>
	  <td align="right">Published: </td>	  
	  <td ><input name="published" type="checkbox" id="published" value="1" <? if ($published>0) echo 'checked' ?> /></td>
	</tr>

    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input name="butSave" type="submit" id="butSave" value="Update" class="button">
        <input name="date_add" type="hidden" id="date_add" value="<?=mktime();?>" />
      </label></td>
    </tr></form>
</table>

